package com.yxhl.zoume.common.presenter;

import android.content.Context;

import com.yxhl.zoume.common.ui.view.BaseView;
import com.yxhl.zoume.common.ui.view.SplashObtionView;
import com.yxhl.zoume.data.http.rest.param.splash.SplashParam;
import com.yxhl.zoume.data.http.rest.response.splash.SplashResponse;
import com.yxhl.zoume.domain.interactor.ZMSubscriber;
import com.yxhl.zoume.domain.interactor.splash.SplashUseCase;
import com.yxhl.zoume.utils.SplashUtils;

import javax.inject.Inject;

/**
 * 项目名称：Ferrari
 * Created by menggod on 16/10/31 .
 */

public class SplashPresenter extends BasePresenter {

    @Inject
    SplashUseCase mSplashUseCase;
    private SplashObtionView splashObtionView;
    private SplashUtils mSplashUtils;

    @Inject
    public SplashPresenter(Context context) {
        super(context);
    }

    public void obtionSplash(String screen) {
        final SplashParam splashParam = new SplashParam();
        splashParam.setScreen(screen);
        mSplashUtils = SplashUtils.getInstance(mContext);
        mSplashUseCase.execute(splashParam,new ZMSubscriber<SplashResponse>() {
            @Override
            public void onCompleted() {
                super.onCompleted();
            }

            @Override
            public void onNext(SplashResponse splashResponse) {
                super.onNext(splashResponse);

                if (splashResponse != null) {
                    String isSucc = splashResponse.isSucc() + "";
                    splashObtionView.renderSplashOnSuccess(splashResponse);
                    if ("true".equalsIgnoreCase(isSucc)) {
                        SplashResponse.DataBean data = splashResponse.getData();
                        if (null != data) {
                            if(null != splashObtionView) {
                                saveUrl(data);
                                splashObtionView.renderSplashOnSuccess(splashResponse);
                            }
                        } else {
                            cleanUrl();
                        }
                    } else {
                        if(null != splashObtionView) {
                            cleanUrl();
                            splashObtionView.renderSplashOnError(splashResponse.getResultMessage());
                        }
                    }
                }
            }

            @Override
            public void onError(Throwable e) {
                super.onError(e);
                if(null != splashObtionView) {
                    splashObtionView.renderSplashOnError(e.getLocalizedMessage());
                }
            }
        });
    }

    @Override
    public void attachView(BaseView view) {
        splashObtionView = (SplashObtionView) view;
    }

    private void saveUrl(SplashResponse.DataBean dataBean) {
        mSplashUtils.saveImgUrl(dataBean.getImgUrl());
        mSplashUtils.saveTargetUrl(dataBean.getTargetUrl());
    }

    private void cleanUrl() {
        mSplashUtils.saveImgUrl("");
        mSplashUtils.saveTargetUrl("");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mSplashUseCase.unsubscribe();
    }
}
